package org.csu.laboratoryequipment.model;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class DataPreprocessing {

    //单线程进行处理
    //对数据进行简单清洗
//    public void data_clean1(int qty) throws IOException {
//        long begin = System.currentTimeMillis();
//        InputStreamReader inStream = new InputStreamReader(new FileInputStream(new File(FilePath.ORIGINAL_FILE_NAME)), "GBK");
//        OutputStreamWriter outStream = new OutputStreamWriter(new FileOutputStream(new File(FilePath.TRANSFER_FILE_NAME)), "UTF-8");
//        BufferedReader br = new BufferedReader(inStream);
//        BufferedWriter bw = new BufferedWriter(outStream);
//        String line = null;
////        while ((line = br.readLine()) != null){
//        for (int i=0;i<qty;i++)
//        {
//            line = br.readLine();
//            //对每一条数据去除前面四个字段
//            line=line.substring(39);
//            //以空格为分隔符划分成单个搜索记录
//            String s[]=line.split("\\s+");
//            //逐行对关键字进行清洗
//            // 匹配URL
//            Pattern patternUrl1 = Pattern.compile(".*(([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+).*");
//            // 匹配域名
//            Pattern patternUrl2 = Pattern.compile(".*(([wW]{3}.)?[A-Za-z0-9]+.com).*");
//            // 匹配手机号
//            Pattern patternPhone = Pattern.compile(".*(((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}).*");
//            // 匹配电子邮件
//            Pattern patternEmail = Pattern.compile(".*([a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+).*");
//            // 匹配非字母、数字和汉字的字符
//            Pattern patternChar = Pattern.compile("[^(a-zA-Z0-9\\u4e00-\\u9fa5)]");
//            // 匹配纯数字
//            Pattern patternNum = Pattern.compile("([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])");
//            for (String text : s) {
//                if (!(patternChar.matcher(text).matches()||patternNum.matcher(text).matches()||patternEmail.matcher(text).matches()||patternPhone.matcher(text).matches()||(patternUrl1.matcher(text).matches())||patternUrl2.matcher(text).matches()))
//                {
//                    bw.append(text);
//                    bw.newLine();
//                }
//            }
//        }
//        bw.close();
//        long finish = System.currentTimeMillis();
//        System.out.println((finish-begin)+"ms");
//    }

    //利用多线程对数据进行读取和操作
    public void data_clean(int qty) throws IOException, ExecutionException, InterruptedException {
        long begin = System.currentTimeMillis();
        List<Future<List<String>>> futures = new ArrayList<>();
        int threadCount = 10; //分为10个线程
        int linesPerThread = qty/10; // 除了最后一个线程，每个线程处理qty/10个线程
        ExecutorService executorService = Executors.newFixedThreadPool(threadCount); //创建线程池

        for (int i = 0; i < threadCount; i++) {
            int start = i * linesPerThread;
            int end = (i + 1) * linesPerThread - 1;
            if (i == threadCount - 1) { // 最后一个线程可能需要处理更多的行数
                end = qty-1;
            }
            List<String> output = new ArrayList<>(); // 创建List用于输出结果
            Future<List<String>> future = executorService.submit(new FileTask(FilePath.ORIGINAL_FILE_NAME, start, end, output));
            futures.add(future);
        }
        executorService.shutdown(); // 不再接受新任务，等待所有提交的任务执行完毕后关闭线程池
        List<String> finalOutput = new ArrayList<>(); // 用于保存最终的输出结果
        for (Future<List<String>> future : futures) {
            List<String> result = future.get(); // 阻塞等待任务完成并获取结果
            finalOutput.addAll(result); // 将每个任务的结果添加到最终的输出结果中
        }
        // 所有线程执行完毕后，将结果写入到文件
        Files.write(Paths.get(FilePath.TRANSFER_FILE_NAME), finalOutput, StandardCharsets.UTF_8);
        long finish = System.currentTimeMillis();
        System.out.println("利用多线程进行数据清洗所花的时间是"+(finish-begin)+"ms");
    }

    //对关键词进行选择
    public ArrayList<String> seedkeywordsselect()
    {
        ArrayList<String>arrayList = new ArrayList<>();
        System.out.println("是否自定义选择种子关键词Y/N");
        Scanner scanner = new Scanner(System.in);
        String result = scanner.nextLine();
        if (result.equals("Y")){
            System.out.println("请输入要选的种子关键字(10~20个,输入Y结尾)");
            String string= scanner.nextLine();
            while (!string.equals("Y"))
            {
                arrayList.add(string);
            }
        } else {
            arrayList.add("大学生");
            arrayList.add("华为");
            arrayList.add("沈阳");
            arrayList.add("快乐");
            arrayList.add("宝马");
            arrayList.add("葡萄");
            arrayList.add("电影");
            arrayList.add("女朋友");
            arrayList.add("苹果");
            arrayList.add("河南");
            arrayList.add("旅游");
        }
        return arrayList;
    }

    public int searchqtyselect()
    //对用户数量，即搜索量进行选择
    {
        System.out.println("请选择搜索数据量(1~100000,直接回车选择默认值100000):");
        Scanner scanner = new Scanner(System.in);
        String string = scanner.nextLine();
        if (string.equals("")){
            return 100000;
        }
        else {
            Double num = Double.parseDouble(string);
            if (!(num <= 100000 && num >= 1 && (num % 1 == 0))) {
                searchqtyselect();

            } else{
                return num.intValue();
            }
        }
        return 0;
    }

}
